﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace Intersoft.Controls.TreeGridAssist
{
    public class TreeGridNodeItem : INotifyPropertyChanged
    {
        #region 字段
        private bool _isExpanded = false;
        private List<TreeGridNodeItem> _children = new List<TreeGridNodeItem>();
        #endregion

        #region 属性
        public string Id { get; set; }
        public TreeGridDataSource Owner { get; set; }
        public object Item { get; private set; }
        public int Level { get; private set; }
        public FrameworkElement VisualTarget { get; set; }

        public List<TreeGridNodeItem> Children
        {
            get
            {
                return _children;
            }
        }

        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    RaisePropertyChanged("IsExpanded");
                    if (Owner != null)
                    {
                        Owner.NotifyIsExpandedChanged(this);
                    }
                }
            }
        }

        public bool HasChildren
        {
            get
            {
                return Children.Count > 0;
            }
        }
        #endregion

        #region 公共方法
        public TreeGridNodeItem(TreeGridDataSource owner, string id, object item)
        {
            Owner = owner;
            Id = id;
            Item = item;
        }

        public void AddChild(TreeGridNodeItem child)
        {
            child.Level = Level + 1;
            Children.Add(child);
        }
        #endregion

        #region INotifyPropertyChanged 成员
        protected void RaisePropertyChanged(string name)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(name));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, args);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}
